package com.ccl.keyassistant.utils;
/*
 *项目名     keyassistant
 *包名       com.ccl.keyassistant.utils
 *文件名     PermissionUtil
 *创建者     ccl
 *创建时间   2019-03-08 18:52
 *注解       运行时权限工具类
 */

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.Toast;

import java.util.List;

public class PermissionUtil {
    /**
     * 判断是否取得了相应的权限
     *
     * @param context    上下文
     * @param permission 要判断的权限名
     * @return true表示获取到了相应的权限
     */
    public static boolean isPermitted(Context context, String permission) {
        int permit = ContextCompat.checkSelfPermission(context, permission);
        if (PackageManager.PERMISSION_GRANTED == permit) {
            return true;
        }
        if (PackageManager.PERMISSION_DENIED == permit) {
            return false;
        }
        return false;
    }

    /**
     * 请求权限
     *
     * @param activity
     * @param permissions 相应的权限
     * @param requestCode 用于识别此权限请求的整型请求代码
     */
    public static void requestPermission(Activity activity, List<String> permissions, int requestCode) {
        if (permissions != null && permissions.size() > 0) {
            //将list装换为String[]
            String[] perm = permissions.toArray(new String[permissions.size()]);

            ActivityCompat.requestPermissions(activity, perm, requestCode);
        }

    }

    /**
     * 请求权限
     *
     * @param activity
     * @param permissions 相应的权限
     * @param requestCode 用于识别此权限请求的整型请求代码
     */
    public static void requestPermission(Activity activity, String[] permissions, int requestCode) {
        if (permissions != null && permissions.length > 0) {
            ActivityCompat.requestPermissions(activity, permissions, requestCode);
        }

    }


}
